$(function(){
    // 获取数据
    var currentPage = 1;
    var pageSize = 5;
    function render() {
        $.ajax({
            type: 'get',
            url: '/user/queryUser',
            data: {
                page: currentPage,
                pageSize: pageSize
            },
            dataType: 'json',
            success: function(res){
                console.log(res);
                $('tbody').html(template('tmp', res));
                setPage(res.total);
            }

        })
    }

    render();

    // 模态框处理
    var id = null;
    var isDelete = null;
    $('tbody').on('click', '.btn-exec', function(){
        id = $(this).parent().data('id');
        isDelete = $(this).hasClass('btn-danger') ? 0 : 1;
        console.log(id,isDelete);
    });
    $('.btn-ok').click(function(){
        $.ajax({
            type: 'post',
            url: '/user/updateUser',
            data: {
                id: id,
                isDelete: isDelete
            },
            dataType: 'json',
            success:function(){
                console.log(res);
                render();
                $('.modal-user').modal('hide');
            }
        })
    });

    // 分页功能
    function setPage(total){
        $('#paginator').bootstrapPaginator({
            bootstrapMajorVersion: 3,
            currentPage: currentPage,
            totalPage: Math.ceil(total /  pageSize),
            onPageClicked: function(event, originalEvent, type, page){
                console.log(page);
                currentPage = page;
                render();
            }
        });
    }
})